package com.unknowpupil.web.tool;

import com.unknowpupil.web.domain.Account;
import org.apache.commons.lang3.StringUtils;

/**
 * @Classname: ConstantsTool
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/11/7 11:26
 * @Description:
 * @Version: 1.0
 */
public class ConstantsTool {
    public static final String HASH_ALGORITHM = "SHA-1";
    /**
     * 生成Hash值的迭代次数
     */
    public static final int HASH_INTERATIONS = 1024;
    /**
     * 生成盐的长度
     */
    public static final int SALT_SIZE = 16;
    /**
     *  设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
     * @param account
     */
    public static void entryptPassword(Account account){
        byte [] salt =DigestsTool.generateSalt(ConstantsTool.SALT_SIZE);
        account.setSalt(EncodesTool.encodeHex(salt));
        byte [] hashPassword = DigestsTool.sha1(account.getPassword().getBytes(),salt,ConstantsTool.HASH_INTERATIONS);
        account.setPassword(EncodesTool.encodeHex(hashPassword));
    }


    /**
     *
     * @param paramStr 输入需要加密的字符串
     * @return
     */
    public static String entryptPassword(String paramStr,String salt) {
        if(StringUtils.isNotEmpty(paramStr)){
            byte[] saltStr = EncodesTool.decodeHex(salt);
            byte[] hashPassword = DigestsTool.sha1(paramStr.getBytes(), saltStr, ConstantsTool.HASH_INTERATIONS);
            String password = EncodesTool.encodeHex(hashPassword);
            return password;
        }else{
            return null;
        }
    }
}
